const Utils = {
  dateFormat: (date) => {
    if (!date) {
      date = new Date();
    }
    let year = date.getFullYear().toString();
    let month = (parseInt(date.getMonth()) + 1).toString();
    let day = date.getDate().toString();
    return year + '-' + (month.length < 2 ? '0' + month : month) + '-' + (day.length < 2 ? '0' + day : day);
  },
  dataHMS: (date) => {
    date = date || new Date();
    let h = date.getHours().toString();
    let m = date.getMinutes().toString();
    let s = date.getSeconds().toString();
    return (h.length < 2 ? '0' + h : h) + ':' + (m.length < 2 ? '0' + m : m) + ':' + (s.length < 2 ? '0' + s : s);
  },
  minDate: (date) => {
    let year = date.getFullYear().toString();
    let month = (date.getMonth() + 1).toString();
    let day = date.getDate().toString();
    let h = date.getHours().toString();
    let m = date.getMinutes().toString();
    let s = date.getSeconds().toString();
    return (
      year +
      '-' +
      (month.length < 2 ? '0' + month : month) +
      '-' +
      (day.length < 2 ? '0' + day : day) +
      ' ' +
      (h.length < 2 ? '0' + h : h) +
      ':' +
      (m.length < 2 ? '0' + m : m)
    );
  },
  maxDate: (date) => {
    let date2 = new Date(date);
    date2.setDate(date.getDate() + 7);
    let year = date2.getFullYear().toString();
    let month = (date2.getMonth() + 1).toString();
    let day = date2.getDate().toString();
    let h = date2.getHours().toString();
    let m = date2.getMinutes().toString();
    let s = date2.getSeconds().toString();
    return (
      year +
      '-' +
      (month.length < 2 ? '0' + month : month) +
      '-' +
      (day.length < 2 ? '0' + day : day) +
      ' ' +
      (h.length < 2 ? '0' + h : h) +
      ':' +
      (m.length < 2 ? '0' + m : m)
    );
  },
  date: (date) => {
    let year = date.getFullYear().toString();
    let month = (date.getMonth() + 1).toString();
    let day = date.getDate().toString();
    let h = date.getHours().toString();
    let m = date.getMinutes().toString();
    let s = date.getSeconds().toString();
    return (
      year +
      '-' +
      (month.length < 2 ? '0' + month : month) +
      '-' +
      (day.length < 2 ? '0' + day : day) +
      ' ' +
      h +
      ':' +
      m
    );
  },
  timestampToTime(timestamp) {
    var date = new Date(timestamp);
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();

    var strDate = Y + M + D + h + m + s;
    return strDate;
  },
  getNowFormatDate() {
    var date = new Date();
    var seperator1 = '-';
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = '0' + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = '0' + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  },
  // 发送消息
  applyMessage() {
    return new Promise<void>((resolve, reject) => {
      if (!Taro.requestSubscribeMessage) {
        resolve()
        return
      }
      //弹窗通知提醒
      let ids = ["7apkUi2hVwYWMiD614VxWTmTQANeooUzlBWVH-PWsJs", "-HwIii8_O_gQFZSuLzqpk-uSA8kjnHGWe2x_nOi1Rk0"]
      Taro.requestSubscribeMessage({
        tmplIds: ids,
        success: (res) => {
          if (res[ids[0]] === "accept" || res[ids[1]] === "accept") {
            Taro.showToast({
              title: "订阅成功！",
              duration: 1000,
              success(data) {
                // 成功
                resolve()
              }
            })
          } else {
            Taro.showModal({
              content: "未授权发送通知，您将收不到通知！",
              confirmText: "重新授权",
              cancelText: "取消授权",
              success: (re) => {
                if (re.confirm) {
                  reject()
                  // this.request_order = false;
                  Utils.applyMessage()
                } else {
                  resolve()
                }
              },
              confirmColor: '#218CFF'
            })
          }
        },
        fail: (err) => {
          Taro.showModal({
            content: "未授权发送通知，请到小程序设置界面开启订阅",
            confirmText: "去开启",
            cancelText: "暂不开启",
            success: (res) => {
              if (res.confirm) {
                reject()
                Taro.openSetting()
              } else {
                resolve()
              }
            },
            confirmColor: '#218CFF'
          })
        }
      })
      // #endif
      resolve();
    }),
  },
  // 是否设置过授权
  openMsg() {
    var that = this;
    // 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
    Taro.getSetting({
      withSubscriptions: true, // 是否获取用户订阅消息的订阅状态，默认false不返回
      success(res) {
        if (res.authSetting["scope.subscribeMessage"]) { // 用户点击了“总是保持以上，不再询问”
          Taro.openSetting({ // 打开设置页
            success(re) {
              console.log(re.authSetting)
            }
          });
        } else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
          Utils.applyMessage();
        }
      }
    })
  }
};

export default Utils;
